using System;
using System.Windows.Forms;

namespace TimeLineControl
{
    public partial class FrmConfigAlert : Form
    {
        private bool _cancel;

        public FrmConfigAlert()
        {
            InitializeComponent();
        }

        private void ConfigAlert_Load(object sender, EventArgs e)
        {
            ChkCus1.Checked = DataConfigTimeLine.AlertTime1.Minutes != 0;
            ChkCus2.Checked = DataConfigTimeLine.AlertTime2.Minutes != 0;
            ChkCus3.Checked = DataConfigTimeLine.AlertTime3.Minutes != 0;
            ChkCus4.Checked = DataConfigTimeLine.AlertTime4.Minutes != 0;
            ChkCus5.Checked = DataConfigTimeLine.AlertTime5.Minutes != 0;
            NUpDownCus1.Maximum = 600;
            NUpDownCus2.Maximum = 600;
            NUpDownCus3.Maximum = 600;
            NUpDownCus4.Maximum = 600;
            NUpDownCus5.Maximum = 600;
            NUpDownCus1.Value = (int)DataConfigTimeLine.AlertTime1.TotalMinutes;
            NUpDownCus2.Value = (int)DataConfigTimeLine.AlertTime2.TotalMinutes;
            NUpDownCus3.Value = (int)DataConfigTimeLine.AlertTime3.TotalMinutes;
            NUpDownCus4.Value = (int)DataConfigTimeLine.AlertTime4.TotalMinutes;
            NUpDownCus5.Value = (int)DataConfigTimeLine.AlertTime5.TotalMinutes;
            TxtCustMsg1.Text = DataConfigTimeLine.MsgAlertTime1;
            TxtCustMsg2.Text = DataConfigTimeLine.MsgAlertTime2;
            TxtCustMsg3.Text = DataConfigTimeLine.MsgAlertTime3;
            TxtCustMsg4.Text = DataConfigTimeLine.MsgAlertTime4;
            TxtCustMsg5.Text = DataConfigTimeLine.MsgAlertTime5;

            NUpDownCus1.Tag = (int)DataConfigTimeLine.AlertTime1.TotalMinutes;
            NUpDownCus2.Tag = (int)DataConfigTimeLine.AlertTime2.TotalMinutes;
            NUpDownCus3.Tag = (int)DataConfigTimeLine.AlertTime3.TotalMinutes;
            NUpDownCus4.Tag = (int)DataConfigTimeLine.AlertTime4.TotalMinutes;
            NUpDownCus5.Tag = (int)DataConfigTimeLine.AlertTime5.TotalMinutes;
        }

        private void Button1Click(object sender, EventArgs e)
        {
            _cancel = true;
            Close();
        }

        private void Button2Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ConfigAlert_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_cancel)
            {
                DataConfigTimeLine.AlertTime1 = new TimeSpan(0, (int)NUpDownCus1.Value, 0);
                DataConfigTimeLine.AlertTime2 = new TimeSpan(0, (int)NUpDownCus2.Value, 0);
                DataConfigTimeLine.AlertTime3 = new TimeSpan(0, (int)NUpDownCus3.Value, 0);
                DataConfigTimeLine.AlertTime4 = new TimeSpan(0, (int)NUpDownCus4.Value, 0);
                DataConfigTimeLine.AlertTime5 = new TimeSpan(0, (int)NUpDownCus5.Value, 0);
                DataConfigTimeLine.MsgAlertTime1 = TxtCustMsg1.Text;
                DataConfigTimeLine.MsgAlertTime2 = TxtCustMsg2.Text;
                DataConfigTimeLine.MsgAlertTime3 = TxtCustMsg3.Text;
                DataConfigTimeLine.MsgAlertTime4 = TxtCustMsg4.Text;
                DataConfigTimeLine.MsgAlertTime5 = TxtCustMsg5.Text;
            }
        }

        private void ChkCus1_CheckedChanged(object sender, EventArgs e)
        {
            if (!ChkCus1.Checked)
            {
                NUpDownCus1.Value = 0;
                NUpDownCus1.Tag = "0";
                TxtCustMsg1.Text = string.Empty;
            }
            else
            {
                if (TxtCustMsg1.Text == string.Empty)
                {
                    TxtCustMsg1.Text = string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                     NUpDownCus1.Value);
                }
            }
        }

        private void ChkCus2_CheckedChanged(object sender, EventArgs e)
        {
            if (!ChkCus2.Checked)
            {
                NUpDownCus2.Value = 0;
                NUpDownCus2.Tag = "0";
                TxtCustMsg2.Text = string.Empty;
            }
            else
            {
                if (TxtCustMsg2.Text == string.Empty)
                {
                    TxtCustMsg2.Text = string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                     NUpDownCus2.Value);
                }
            }
        }

        private void ChkCus3_CheckedChanged(object sender, EventArgs e)
        {
            if (!ChkCus3.Checked)
            {
                NUpDownCus3.Value = 0;
                NUpDownCus3.Tag = "0";
                TxtCustMsg3.Text = string.Empty;
            }
            else
            {
                if (TxtCustMsg3.Text == string.Empty)
                {
                    TxtCustMsg3.Text = string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                     NUpDownCus3.Value);
                }
            }
        }

        private void ChkCus4_CheckedChanged(object sender, EventArgs e)
        {
            if (!ChkCus4.Checked)
            {
                NUpDownCus4.Value = 0;
                NUpDownCus4.Tag = "0";
                TxtCustMsg4.Text = string.Empty;
            }
            else
            {
                if (TxtCustMsg4.Text == string.Empty)
                {
                    TxtCustMsg4.Text = string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                     NUpDownCus4.Value);
                }
            }
        }

        private void ChkCus5_CheckedChanged(object sender, EventArgs e)
        {
            if (!ChkCus5.Checked)
            {
                NUpDownCus5.Value = 0;
                NUpDownCus5.Tag = "0";
                TxtCustMsg5.Text = string.Empty;
            }
            else
            {
                if (TxtCustMsg5.Text == string.Empty)
                {
                    TxtCustMsg5.Text = string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                     NUpDownCus5.Value);
                }
            }
        }

        private void NUpDownCus1_ValueChanged(object sender, EventArgs e)
        {
            if (TxtCustMsg1.Text == string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                                 NUpDownCus1.Tag) && ChkCus1.Checked)
            {
                TxtCustMsg1.Text = string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                  NUpDownCus1.Value);
            }
            NUpDownCus1.Tag = NUpDownCus1.Value.ToString();
        }

        private void NUpDownCus2_ValueChanged(object sender, EventArgs e)
        {
            if (TxtCustMsg2.Text == string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                     NUpDownCus2.Tag) && ChkCus2.Checked)
            {
                TxtCustMsg2.Text = string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                  NUpDownCus2.Value);
            }
            NUpDownCus2.Tag = NUpDownCus1.Value.ToString();
        }

        private void NUpDownCus3_ValueChanged(object sender, EventArgs e)
        {
            if (TxtCustMsg3.Text == string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                     NUpDownCus3.Tag) && ChkCus3.Checked)
            {
                TxtCustMsg3.Text = string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                  NUpDownCus3.Value);
            }
            NUpDownCus3.Tag = NUpDownCus3.Value.ToString();
        }

        private void NUpDownCus4_ValueChanged(object sender, EventArgs e)
        {
            if (TxtCustMsg4.Text == string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                     NUpDownCus4.Tag) && ChkCus4.Checked)
            {
                TxtCustMsg4.Text = string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                  NUpDownCus4.Value);
            }
            NUpDownCus4.Tag = NUpDownCus4.Value.ToString();
        }

        private void NUpDownCus5_ValueChanged(object sender, EventArgs e)
        {
            if (TxtCustMsg5.Text == string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                     NUpDownCus5.Tag) && ChkCus5.Checked)
            {
                TxtCustMsg5.Text = string.Format(ScreenUtil.StringResource("MsgDefaultAlert"),
                                                  NUpDownCus5.Value);
            }
            NUpDownCus5.Tag = NUpDownCus5.Value.ToString();
        }
   }
}